package com.guigu.health.service.impl;

import com.guigu.health.dao.UserRolePermissionMapper;
import com.guigu.health.pojo.Permission;
import com.guigu.health.pojo.UserRolePermission;
import com.guigu.health.service.UserRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName:UserRolePermissionServiceImpl
 * @Description:
 * @Author:longtao
 * @Date:2021/4/9
 * @Version:1.0
 */
@Service("userRolePermissionService")
public class UserRolePermissionServiceImpl implements UserRolePermissionService {

    private UserRolePermissionMapper userRolePermissionMapper;
    @Autowired
    @Qualifier("userRolePermissionMapper")
    public void setUserRolePermissionMapper(UserRolePermissionMapper userRolePermissionMapper) {
        this.userRolePermissionMapper = userRolePermissionMapper;
    }

    @Override
    public List<UserRolePermission> queryAllByUserID(String account) {
        return userRolePermissionMapper.queryAllByUserID(account);
    }

    /**
     * 根据角色id查询出本角色的权限关系
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> findPermissionID(Integer id) {
        List<Integer> list = userRolePermissionMapper.findPermissionID(id);
        return list;
    }

    /**
     * 显示所有的权限列表
     *
     * @return
     */
    @Override
    public List<Permission> queryAllPermission() {
        return userRolePermissionMapper.queryAllPermission();
    }

    /**
     * 修改角色和用户之间的关系
     *
     * @param id
     * @param permissionIds
     */
    @Override
    @Transactional
    public void updatePermission(Integer id, Integer[] permissionIds) {
        /**
         * 先进行删除,在进行修改
         */
        userRolePermissionMapper.deletePermission(id);
        if (null!=permissionIds){
            for (Integer permissionId : permissionIds) {
                userRolePermissionMapper.addPermission(id,permissionId);
            }
        }
    }
}
